package com.flex.showdoc4j.executor;

import com.flex.showdoc4j.config.Configuration;
import com.flex.showdoc4j.model.DocModel;
import com.flex.showdoc4j.model.ParamModel;
import com.flex.showdoc4j.model.ReturnVariableModel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author ganlt<br>
 * @since 2019/06/26 14:58 <br>
 */
@ToString(callSuper = true)
public abstract class Wrapper {
    public static final String RETURN_PAGE_ATTRIBUTE_NAME = "list";
    public static final String RETURN_ATTRIBUTE_NAME = "content";
    public static final String RPARAM_ATTRIBUTE_NAME = "content";
    private static final String URL_SEPARATOR = "/";
    @Setter
    @Getter
    private Configuration configuration;
    @Setter
    @Getter
    private String patternUrl;
    @Setter
    @Getter
    private String retrunJsonStr;
    @Setter
    @Getter
    private String requestParamJsonStr;

    /**
     * 获取文档
     * @return 获取文档
     */
    public abstract DocModel getDocModel();

    /**
     * 获取文档参数
     * @return 获取文档参数
     */
    public abstract List<ParamModel> paramModelList();
    
    @Setter
    @Getter
    public List<ReturnVariableModel> returnParamMap = new ArrayList<>();
    /**
     * 获取url
     * @return 获取url
     */
    public String getUrl() {
        String baseUrl = Optional.ofNullable(getConfiguration().getConfigModel().getBaseUrl()).orElse("");
        StringBuilder url = new StringBuilder(baseUrl);
        if (!StringUtils.isEmpty(getDocModel().getUrl())) {
            url.append(getDocModel().getUrl());
        } else {
            //从pattern获取
            url.append(separaterUrl(getPatternUrl()));
        }
        return url.toString();
    }
    /**
     * 分割符隔开的url
     * @param url 隔开的url
     * @return 分割符隔开的url
     */
    private String separaterUrl(String url) {
        if (url.startsWith(URL_SEPARATOR)) {
            return url;
        }
        return URL_SEPARATOR + url;
    }

}
